<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="_header_include::frag(~{::title},~{},~{})">
    <title>后台管理系统</title>
</head>
<body>
<div id="maindiv" class="maindiv mainPadding" style="display: none;">    
    <el-breadcrumb separator="/" class="elBreadcrumb marginB20">
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>数据报表</el-breadcrumb-item>
        <el-breadcrumb-item>重单表</el-breadcrumb-item>
    </el-breadcrumb>    
    <el-row class="marginB20">
        <el-button type="primary" @click="searchMonth">月统计</el-button>
        <el-button type="success" @click="searchSeason">季统计</el-button>
        <el-button type="warning" @click="searchYear">年统计</el-button> 
        <!-- <shiro:hasPermission name="statistics:businessVisitReceptionTable:export"> -->
        <el-button type="danger" @click="exportRs">导出Excel报表</el-button>
        <!-- </shiro:hasPermission> -->
    </el-row>
    <div class="mainSearchBg">        
        <div class="mainSearchFormBox">
           <el-form :inline="true" :model="form" class="demo-form-inline mainSearchForm" ref="form">
                <el-form-item label="统计日期：">
                    <el-date-picker format="yyyy-MM-dd 00:00" v-model="form.startTime" type="date" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="—" class="widthauto">
                    <el-date-picker format="yyyy-MM-dd 23:59" v-model="form.endTime" type="date" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="签约餐饮集团：">  
                    <el-select v-model="form.companyId" placeholder="全部" style="width: 100%;" clearable filterable>
                        <el-option
                            v-for="item in companyList"
                            :key="item.id"
                            :label="item.groupName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="签约项目：">  
                    <el-select v-model="form.projectId" placeholder="全部" style="width: 100%;" clearable filterable>
                        <el-option
                            v-for="item in projectList"
                            :key="item.id"
                            :label="item.projectName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="事业部：">  
                        <el-select v-model="form.teleDeptId" placeholder="全部" style="width: 100%;" clearable filterable @change="curTelGroupSale" :disabled="isDisabled">
                            <el-option
                                v-for="item in deptList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="电销组：">  
                        <el-select v-model="form.teleGroupId" placeholder="全部" style="width: 100%;" clearable filterable  :disabled="isDisabledOrgId">
                            <el-option
                                v-for="item in orgTeleArr"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="签约店型：">  
                        <el-select v-model="form.shopTypes" placeholder="全部" style="width: 100%;" clearable filterable multiple>
                            <el-option
                                v-for="item in shopTypeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="签约类型：">  
                        <el-select v-model="form.signType" placeholder="全部" style="width: 100%;" clearable filterable>
                            <el-option
                                v-for="item in signTypeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                <div class="mainSearchBtnBox">
                    <el-button icon="el-icon-search" type="primary" @click="searchTable(1)" class="searchBtn" :disabled="loading">搜索</el-button>
                </div>  
            </el-form>
        </div>
        <el-row>
            <el-table 
                ref="tableData"
                tooltip-effect="dark"
                style="width: 1200px;"
                border
                :data="tableData"
                v-loading="loading"
                :cell-style="centerStyle"
                :header-cell-style="centerStyle"
                :span-method="arraySpanMethod">
                <el-table-column type="index" label="序号" width="55" :index="indexFun"></el-table-column>
                <el-table-column prop="teleGroupName" label="电销组">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" v-if="scope.row.teleGroupId!='99999'" @click="gotoTeam(scope.row)">{{scope.row.teleGroupName}}</el-button>
                        <span v-else>{{scope.row.teleGroupName}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="signNum" label="签约单数"></el-table-column>
                <el-table-column prop="selfSign" label="其中：自签约"></el-table-column>
                <el-table-column prop="dupSign" label="其中：重单签约"></el-table-column>
                <el-table-column prop="dupRate" label="重单率"></el-table-column>
            </el-table>
            <table-pagination :pager="pager" @change="searchTable()"></table-pagination>
        </el-row>
    </div>
</div>
<!-- import common js-->
<div th:include="_footer_include :: #jsLib"></div>
</body>
<style>
    .el-table tr:nth-of-type(1) td:nth-of-type(n+1) {font-weight: bold;}
    .mainSearchFormBox form .el-form-item:nth-child(3) .el-form-item__label{
        width: 130px;
    }
</style>
<script th:inline="javascript">
    var companyList=[[${companyList}]]; // 签约餐饮集团
    var projectList=[[${projectList}]];// 签约项目
    var shopTypeList=[[${shopTypeList}]];// 签约店型
    var deptList=[[${deptList}]];// 事业部
    var curDeptId=[[${deptId}]]; // 当前用户事业部Id
    var curOrgId=[[${teleGroupId}]]; // 当前用户电销组id
    var mainDivVM = new Vue({
        el: '#maindiv',
        data: function() {
            return {
                loading: false,
                isDisabled: false,
                isDisabledOrgId: false,
                pager:{
                    total: 0,
                    currentPage: 1,
                    pageSize: 20,
                },
                form: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: '',
                    teleGroupId: '',
                    companyId:'',
                    projectId: '',
                    signType: '',
                    shopTypes: [] 
                },
                export: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: '',
                    teleGroupId: '',
                    companyId: '',
                    projectId: '',
                    signType: '',
                    shopTypes:[],
                },
                tableData: [],
                deptList: deptList, // 电销事业部
                orgTeleArr: '', // 商务组
                busSaleArr: '', // 商务经理
                companyList: companyList,
                projectList: projectList,
                shopTypeList: shopTypeList,
                signTypeList: [{id: 1,name: '一次性全款'},{id: 2,name: '先交定金'}]
            }             
        },
        methods: {    
            centerStyle(){
                return "text-align:center"
            },
            firstCurTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelSale(seleVal);
            },
            curTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleSaleId = '';
                this.getTelSale(seleVal);
            },
            firstCurTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelGroupSale(seleVal);
            },
            curTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleGroupId = '';
                this.form.teleSaleId = '';
                this.getTelGroupSale(seleVal);
            },
            getTelGroupSale(seleVal){
                param={};
                param.parentId=seleVal;
                param.orgType=1;
                axios.post('/base/getGroupList',param).then(function (response) {
                    mainDivVM.orgTeleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销组请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            getTelSale(seleVal){
                param={};
                param.orgId=seleVal;
                param.roleCode="DXCYGW";
                axios.post('/base/getSaleList' ,param).then(function (response) {  
                    mainDivVM.busSaleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销顾问请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            searchTable(firstPage) { 
                var startTime = this.form.startTime;
                var endTime = this.form.endTime;
                if(endTime && startTime){
                    startTime = new Date(this.form.startTime);
                    endTime = new Date(this.form.endTime);
                    if(startTime>endTime){
                        this.$message({
                            message: '开始时间不能大于结束时间',
                            type: 'warning'
                        });
                        return;
                    }
                    var date = new Date();
                    var currentDate = moment(date).format("YYYY-MM-DD 23:59");
                    var currentDateRes = new Date(currentDate);
                    if(endTime > currentDateRes){
                        this.$message({
                            message: '请选择当前日期前的时间',
                            type: 'warning'
                        });
                        return;
                    }
                    startTime = this.limitTime(startTime, null);
                    endTime = this.limitTime(null, endTime);
                }else{
                    this.$message({
                        message: '请选择起止时间',
                        type: 'warning'
                    });
                    return;
                }
    
                var param = {};
                param.startTime = this.minuteFormat(startTime);
                param.endTime = this.minuteFormat(endTime);
                param.pageSize = this.pager.pageSize;
  
                if(firstPage){
                    param.pageNum = firstPage;
                }else{
                    param.pageNum = this.pager.currentPage;
                }
                param.teleDeptId = this.form.teleDeptId;
                param.teleGroupId = this.form.teleGroupId;
                param.companyId = this.form.companyId;
                param.projectId = this.form.projectId;
                param.signType = this.form.signType;
                param.shopTypes = this.form.shopTypes;
                this.loading = true;
                axios.post('/repetition/queryPage',param).then(function (response) {
                    if(!response || !response.data){
                        mainDivVM.$message({
                            message: "接口调用失败",
                            type: 'error'
                        });
                        return ;
                    }
                    var resobj= response.data;
                    if(resobj.code != '0'){
                        mainDivVM.loading = false;
                        mainDivVM.$message({
                            message: resobj.msg,
                            type: 'error'
                        });
                        return ;
                    }else if(resobj.data && resobj.data.tableData){
                        var resobjData = resobj.data;    
                        var tableData = resobjData.tableData;
                        mainDivVM.tableData = tableData.data;
                        if( tableData.data.length && resobj.data.totalData ){
                            var totalData = resobjData.totalData[0];
                            mainDivVM.tableData.unshift(totalData);
                        }
                        mainDivVM.pager.currentPage = tableData.currentPage;
                        mainDivVM.pager.total = tableData.total;
                        mainDivVM.loading = false;
                        mainDivVM.export.startTime = mainDivVM.minuteFormat(startTime);
                        mainDivVM.export.endTime = mainDivVM.minuteFormat(endTime);
                        mainDivVM.export.teleDeptId = mainDivVM.form.teleDeptId;
                        mainDivVM.export.teleGroupId = mainDivVM.form.teleGroupId;
                        mainDivVM.export.companyId = mainDivVM.form.companyId;
                        mainDivVM.export.projectId = mainDivVM.form.projectId;
                        mainDivVM.export.signType = mainDivVM.form.signType;
                        mainDivVM.export.shopTypes = mainDivVM.form.shopTypes;
                        
                    }else{
                        mainDivVM.loading = false;
                    }
                }).catch(function (error) {
                    mainDivVM.loading = false;
                    mainDivVM.$message({
                        message: "请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            limitTime (start, end) { // 禁止选择分钟
                var endTime = this.minuteFormat (end);
                var startTime = this.minuteFormat (start);
                var date = new Date();
                data = this.minuteFormat (date);// 现在的时间
                data = data.slice(0,8);
                if(startTime){
                    this.form.startTime = moment(start).format("YYYY-MM-DD 00:00");
                    return this.form.startTime;
                }
                if(endTime){
                    this.form.endTime = moment(end).format("YYYY-MM-DD 23:59");
                    return this.form.endTime;   
                }
            },
            gotoTeam(row){
                var teleGroupId = row.teleGroupId;
                var teleDeptId = this.export.teleDeptId;
                var companyId = this.export.companyId;
                var startTime = this.export.startTime;
                var endTime = this.export.endTime;
                var signType=this.export.signType;
                var shopTypes=this.export.shopTypes;
                var strSignStore="";
                if(shopTypes.length){
                    strSignStore=shopTypes.join(",");
                } 
                if(teleGroupId){
                    window.location.href="/repetition/repetitionTableTeam?teleDeptId=" + teleDeptId + "&teleGroupId="+ teleGroupId + "&companyId="+ companyId +"&startTime=" + startTime + "&endTime=" + endTime+ "&signType="+ signType + "&strSignStore="+ strSignStore; 
                }
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合并第一行
                if (rowIndex === 0) {
                    if (columnIndex === 0) {
                        return [0, 0];
                    } else if (columnIndex === 1) {
                        return [1, 2];
                    }
                }
            },
            indexFun (index) { // 表格序号
                return index;
            },
            minuteFormat (m) {
                if (!m) {return "";}
                var timeStr = new Date(m);
                var yearRes = timeStr.getFullYear()
                var monthRea = timeStr.getMonth()+1;
                monthRea = monthRea < 10 ?  "0" + monthRea : monthRea;
                var dateRes = timeStr.getDate();
                dateRes = dateRes < 10 ?  "0" + dateRes : dateRes;  
                var hoursRes = timeStr.getHours();
                hoursRes = hoursRes < 10 ?  "0" + hoursRes : hoursRes;  
                var minRes = timeStr.getMinutes();
                minRes = minRes < 10 ?  "0" + minRes : minRes;                
                var timeRes = "" + yearRes + monthRea + dateRes + hoursRes + minRes;
                return timeRes;
            },
            searchSeason(){ // 季
                var today = new Date();
                var month = today.getMonth()+1;
                var startTime = '';
                switch (month) {
                    case 1 : 
                    case 2 : 
                    case 3 : startTime = today.getFullYear()+"-" + 01 + "-" + 01+" 00:00";break;
                    case 4 : 
                    case 5 : 
                    case 6 : startTime = today.getFullYear()+"-" + 04 + "-" + 01+" 00:00"; break;
                    case 7 : 
                    case 8 : 
                    case 9 : startTime = today.getFullYear()+"-" + 07 + "-" + 01+" 00:00";break;
                    case 10 :
                    case 11 :
                    case 12 : startTime = today.getFullYear()+"-" + 10 + "-" + 01+" 00:00";break;
                }
               var endTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchYear(){ // 年
               var today = new Date();
               var startTime = today.getFullYear()+"-" + 01 + "-" + 01+" 00:00";
               var endTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchMonth(){ // 本月 
                var a = new Date();
                var year = a.getFullYear();
                var month = a.getMonth();
                var date = a.getDate();
                var b = new Date(year,month,date);
                var f= new Date(year,month,01);
                var c = f.valueOf();
                var startTime= new Date(c);
                this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                this.form.endTime=year+"-"+(month+1)+"-"+date+" 23:59";
                this.searchTable(1);
            },
            exportRs(){
                var param = {};
                param.startTime = this.export.startTime;
                param.endTime = this.export.endTime;
                param.teleDeptId = this.export.teleDeptId;
                param.teleGroupId = this.export.teleGroupId;
                param.companyId = this.export.companyId;
                param.projectId = this.export.projectId;
                param.signType = this.export.signType;
                param.shopTypes = this.export.shopTypes;
                axios.post('/repetition/export',param,{responseType:'blob'})
                .then(function (response) {
                    var data =  response.data;
                    var fileName = response.headers.filename;
                    saveAs(data,decodeURI(fileName));
                })
                .catch(function (error) {
                    mainDivVM.$message({
                        message: "导出失败，请稍后重试",
                        type: "error"
                    });
                });
            }
        },
        created(){ 
            var today = new Date();
            var startTime = moment(today).format("YYYY-MM-DD 00:00");
            var endTime = moment(today).format("YYYY-MM-DD 23:59");
            this.form.startTime=startTime;
            this.form.endTime=endTime; 
            if(curDeptId){
                this.isDisabled = true;
                this.form.teleDeptId = curDeptId;
                this.export.teleDeptId = this.form.teleDeptId;
                this.firstCurTelGroupSale(curDeptId);
                if(curOrgId){
                    this.isDisabledOrgId = true;
                    this.form.teleGroupId = curOrgId;
                    this.export.teleGroupId = this.form.curOrgId;
                }
            }
            this.export.startTime = this.minuteFormat(this.form.startTime);
            this.export.endTime = this.minuteFormat(this.form.endTime);

            var localVal=localStorage.getItem('allChangePageSize')?parseInt(localStorage.getItem('allChangePageSize')):'';
            if(localVal){this.pager.pageSize = localVal;}

            this.searchTable();
        },
        mounted(){
            document.getElementById('maindiv').style.display = 'block';
        }
    })
    
</script>
</html>